Script to visualize seasonal UGGA data Author: Abigail Lewis Created: 20 December 2023 Last edit: 17 October 2024 (ABP)

# Use the qaqc function to make the current data frame

current_df <- ugga_qaqc(
  files = "./../../../../DataNotYetUploadedToEDI/UGGA/UGGA_Raw/",
  maintenance_file = "https://raw.githubusercontent.com/CareyLabVT/Reservoirs/master/Data/DataNotYetUploadedToEDI/UGGA/UGGA_Maintenance_Log.csv",
  outfile = NULL,
  start_date =NULL, # change when we update to read date from EDI
  end_date = NULL)
## Warning: Using an external vector in selections was deprecated in tidyselect 1.1.0.
## ℹ Please use `all_of()` or `any_of()` instead.
##   # Was:
##   data %>% select(colname_start)
## 
##   # Now:
##   data %>% select(all_of(colname_start))
## 
## See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Using an external vector in selections was deprecated in tidyselect 1.1.0.
## ℹ Please use `all_of()` or `any_of()` instead.
##   # Was:
##   data %>% select(colname_end)
## 
##   # Now:
##   data %>% select(all_of(colname_end))
## 
## See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
#Plot all CH4
current_df%>%
  ggplot(aes(x = as.Date(Date), 
             color = as.factor(Site),
             y=CH4Flux_umolCm2s))+
  geom_point()+
  ylab("CH4 flux (µumol/m2/s)")+
  facet_wrap(~Reservoir)+
  theme_bw()

#by day of year
current_df%>%
  mutate(yday = yday(Date)) %>%
  ggplot(aes(x = yday, 
             color = as.factor(Site),
             shape = as.factor(Flag_CH4Flux_umolCm2s),
             y=CH4Flux_umolCm2s))+
  geom_point()+
  ylab("CH4 flux (µumol/m2/s)")+
  facet_wrap(~Reservoir)+
  theme_bw()

#one negative methane flux in 2020 FCR
#Plot all CO2
current_df%>%
  ggplot(aes(x = as.Date(Date), 
             color = as.factor(Site),
             y=CO2Flux_umolCm2s))+
  geom_point()+
  ylab("CO2 flux (µumol/m2/s)")+
  facet_wrap(~Reservoir)+
  theme_bw()

#by day of year
current_df%>%
  mutate(yday = yday(Date)) %>%
  ggplot(aes(x = yday, 
             color = as.factor(Site),
             shape = as.factor(Flag_CO2Flux_umolCm2s),
             y=CO2Flux_umolCm2s))+
  geom_point()+
  ylab("CO2 flux (µumol/m2/s)")+
  facet_wrap(~Reservoir)+
  theme_bw()

#Plot all CH4
current_CH4 <- current_df%>%
  filter(Date > current_starttime & Date < current_endtime) %>%
  ggplot(aes(x = as.Date(Date), 
             color = as.factor(Site),
             y=CH4Flux_umolCm2s))+
  geom_point()+
  ylab("CH4 flux (µumol/m2/s)")+
  facet_wrap(~Reservoir)+
  theme_bw()

# Make plotly plot

plotly::ggplotly(current_CH4)
#Plot all C02
Current_C02 <- current_df%>%
  filter(Date > current_starttime & Date < current_endtime) %>%
  ggplot(aes(x = as.Date(Date), 
             color = as.factor(Site),
             y=CO2Flux_umolCm2s))+
  geom_point()+
  ylab("C02 flux (µumol/m2/s)")+
  facet_wrap(~Reservoir)+
  theme_bw()

# Make plotly plot c02

plotly::ggplotly(Current_C02)
# Double Check naming convention
# Variable_StartYear_EndYear

# convert datetimes to characters so that they are properly formatted in the output file
# current_df$DateTime <- as.character(format(current_df$Date))

write.csv(current_df, "ugga_2018_2024.csv", row.names = FALSE)
# Maintenance Log
download.file("https://raw.githubusercontent.com/CareyLabVT/Reservoirs/refs/heads/master/Data/DataNotYetUploadedToEDI/UGGA/UGGA_Maintenance_Log.csv", "ugga_maintenancelog_2018_2024.csv")

# qaqc function
download.file("https://raw.githubusercontent.com/CareyLabVT/Reservoirs/refs/heads/master/Scripts/L1_functions/UGGA_create.R", "ugga_qaqc_2018_2024.R")

# helper script to process the UGGA files
download.file("https://raw.githubusercontent.com/CareyLabVT/Reservoirs/refs/heads/master/Data/DataNotYetUploadedToEDI/UGGA/UGGA_Raw/2024/FluxCalR_2024.R", "ugga_FluxCalR_2024.R")
#Install the required googlesheets4 package
#install.packages('googlesheets4')
#Load the library
#read in site description spreadsheet
sites <- gsheet::gsheet2tbl('https://docs.google.com/spreadsheets/d/1TlQRdjmi_lzwFfQ6Ovv1CAozmCEkHumDmbg_L4A2e-8/')
#rename dataframe
data <- current_df
#create site trim function
trim_sites = function(data,sites){
  data_res_site=data%>% #Create a Reservoir/Site combo column
    mutate(res_site = trimws(paste0(Reservoir,Site)))
  sites_merged = sites%>% #Filter to Sites that are in the dataframe
    mutate(res_site = trimws(paste0(Reservoir,Site)))%>%
    filter(res_site%in%data_res_site$res_site)%>%
    select(-res_site)
}
sites_trimmed = trim_sites(data,sites)
write.csv(sites_trimmed,"site_descriptions.csv", row.names=F)# Write to file